package com.blb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.*;
import com.blb.mapper.*;
import com.blb.service.IRoleMenuService;
import com.blb.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.utils.UUIDUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tb
 * @since 2020-05-20
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private IRoleMenuService roleMenuService;
    @Override
    public void updateStatus(String statues, String id) {
        roleMapper.updateStatus(statues,id);
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @Transactional
    @Override
    public int addRole(Role role,String[] menuIds) {
        roleMapper.addRole(role);
        //添加角色菜单关联
        if (menuIds != null){
            for (String menuId:menuIds){
                if (menuId != null){
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setId(UUIDUtils.getId());
                    roleMenu.setRoleId(role.getId());
                    roleMenu.setMenuId(menuId);
                    roleMenuMapper.insert(roleMenu);
                }
            }
        }

        return 1;
    }

    /**
     * 删除角色信息
     * @param roleId
     * @return
     */
    @Transactional
    @Override
    public int deleteRole(String roleId) {
        System.out.println("批量删除id"+roleId);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        //删除用户角色关联表的信息
        userRoleMapper.delete(new QueryWrapper<UserRole>().eq("role_id",roleId ));
        //删除角色表
        roleMapper.deleteById(roleId);
        //删除角色菜单关联表
        roleMenuMapper.delete(new QueryWrapper<RoleMenu>().eq("role_id",roleId ));
        return 0;
    }
    /**
     * 模糊查询
     * @param roleName
     * @return
     */
    @Override
    public List<Role> selectRoleByRoleName(String roleName) {
        return roleMapper.selectRoleByRoleName(roleName);
    }

    /**
     * 角色名是否存在
     * @param roleName
     * @param companyId
     * @return
     */
    @Override
    public Role roleNameisExist(String roleName, String companyId) {
        return roleMapper.roleNameisExist(roleName,companyId);
    }

    @Override
    public Role selectOneRole(String id, String companyId) {
        return roleMapper.selectOneRole(id,companyId);
    }

    /**
     * 修改角色信息
     * @param role
     * @param id
     * @param menuIds
     * @return
     */
    @Transactional
    @Override
    public int updateRole(Role role, String id, String[] menuIds) {
        roleMapper.updateById(role);
        List<RoleMenu> list = roleMenuService.list(new QueryWrapper<RoleMenu>().eq("role_id", id));
        for (RoleMenu RoleMenu : list){
            roleMenuService.removeById(RoleMenu.getId());
        }
        //删除用户菜单关联
        if (menuIds != null){
            for (String menuId:menuIds){
                if (menuId != null){
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setId(UUIDUtils.getId());
                    roleMenu.setRoleId(id);
                    roleMenu.setMenuId(menuId);
                    roleMenuMapper.insert(roleMenu);
                }
            }
        }
        return 1;
    }

    @Override
    public List<Role> getPageRole(Integer page, Integer limit,String comanyId) {
        return roleMapper.getPageRole(page, limit,comanyId);
    }

    @Override
    public int getPageRoleCount(String comanyId){
        return roleMapper.getPageRoleCount(comanyId);
    }
}
